package com.richapp.launcher.scene.conditions.time;

import java.util.Calendar;
import java.util.Date;

import com.richapp.launcher.database.system.dao.SceneModelDao;
import com.richapp.launcher.database.system.mode.SceneModel;
import com.richapp.launcher.scene.SceneServer;
import com.richapp.launcher.scene.abstracts.SceneAbstractTimeConditions;
import com.richapp.launcher.scene.interfaces.ISceneConditions;
import com.richapp.launcher.scene.interfaces.ISceneTimeConditions;

public class  SceneTimeDimmingConditions extends SceneAbstractTimeConditions {

	/**
	 * 定时模式
	 */
	public static final int SCENE_CONDITION_TIME_TIMING_MODEL_DAY = 0;
	public static final int SCENE_CONDITION_TIME_TIMING_MODEL_WEEK = 1;
	public static final int SCENE_CONDITION_TIME_TIMING_MODEL_MONTH = 2;
	
	private int dimmingModelType = SCENE_CONDITION_TIME_TIMING_MODEL_DAY;
	
	private int hours;
	private int minutes;
	private int seconds;
	
	//每周中的星期
	private int[] weekDays;
	//每月中的号数
	private int[] monthDays;
	
	//每天:时分秒
	
	//每周:星期N，时分秒
	
	//每月:N号，时分秒
	
	public SceneTimeDimmingConditions() {
		
	}

	@Override
	public int getConditionType() {
		return ISceneConditions.SCENE_CONDITION_TYPE_TIME;
	}

	@Override
	public int getConditionSubType() {
		return ISceneTimeConditions.SCENE_CONDITION_SUB_TYPE_TIME_TIMING;
	}

	@Override
	public boolean checkCondition(String sceneName) {
		boolean checkFlag=false;
		SceneModel sceneModel = new SceneModel();
		SceneModelDao sceneModelDao = new SceneModelDao(SceneServer.getInstance().getContext());
		sceneModel=sceneModelDao.getSceneModelOnly(sceneName);
		Calendar date = Calendar.getInstance();
		date.setTime(new Date());
		
		switch (dimmingModelType) {
		case SCENE_CONDITION_TIME_TIMING_MODEL_DAY:	//每天:时分秒
			int hour0 = date.get(Calendar.HOUR_OF_DAY);
			int min0 = date.get(Calendar.MINUTE);
			int second0 = date.get(Calendar.SECOND);
			if(hours == hour0 && minutes == min0 && "true".equals(sceneModel.getStatusCycleFlag())
//					&& second0 < (seconds+1)
//					&& second0 > (seconds-1)
					){
				checkFlag= true;
			}else if (hour0 > hours){
			sceneModel.setStatusCycleFlag("true");
			sceneModelDao.add(sceneModel);
		}
			break;
			
		case SCENE_CONDITION_TIME_TIMING_MODEL_WEEK:	//每周:星期N，时分秒
			int week = date.get(Calendar.DAY_OF_WEEK);
			boolean flag1 = false;
			for(int i : weekDays){
				if(i == week){
					flag1 = true;
					break;
				}
			}
			if(flag1){
				int hour1 = date.get(Calendar.HOUR_OF_DAY);
				int min1 = date.get(Calendar.MINUTE);
				int second1 = date.get(Calendar.SECOND);
				if(hours == hour1 && minutes == min1
//						&& second1 < seconds+1
//						&& second1 > seconds -1
						){
					checkFlag = true;
				}
			}
			break;
			
		case SCENE_CONDITION_TIME_TIMING_MODEL_MONTH:	//每月:N号，时分秒
			int day = date.get(Calendar.DAY_OF_MONTH);
			boolean flag2 = false;
			for(int i : monthDays){
				if(i == day){
					flag2 = true;
					break;
				}
			}
			if(flag2){
				int hour2 = date.get(Calendar.HOUR_OF_DAY);
				int min2 = date.get(Calendar.MINUTE);
				int second2 = date.get(Calendar.SECOND);
				if(hours == hour2
						&& minutes == min2
//						&& second2 < seconds+1
//						&& second2 > seconds -1
						){
					checkFlag = true;
				}
			}
			break;
			
		default:
			break;
		}
		
		return checkFlag;
	}
	public int getHours() {
		return hours;
	}

	public void setHours(int hours) {
		this.hours = hours;
	}

	public int getMinutes() {
		return minutes;
	}

	public void setMinutes(int minutes) {
		this.minutes = minutes;
	}

	public int getSeconds() {
		return seconds;
	}

	public void setSeconds(int seconds) {
		this.seconds = seconds;
	}

	public int[] getWeekDays() {
		return weekDays;
	}

	public void setWeekDays(int[] weekDays) {
		this.weekDays = weekDays;
	}

	public int[] getMonthDays() {
		return monthDays;
	}

	public void setMonthDays(int[] monthDays) {
		this.monthDays = monthDays;
	}

	public int getDimmingModelType() {
		return dimmingModelType;
	}

	public void setDimmingModelType(int dimmingModelType) {
		this.dimmingModelType = dimmingModelType;
	}
}
